/* Series simules: AR(p)  */



data exp1;

c=0.5; phi1 = 0.2; phi2 = 0.6; x_t_1 = 0; x_t_2 = 0;

do i= -100 to 400;

	eps_t = rannor(32565);

	x_t   = c + phi1*x_t_1 + phi2*x_t_2 + eps_t;

	if i > 0 then output;

	x_t_2 = x_t_1;

	x_t_1 = x_t;

	end;

run;





/* Graphique d'un AR(2) */

proc gplot data=exp1;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'AR(2): X_t=0.5 + 0.2*X_(t-1) + 0.6*X_(t-2) + eps_t');

	symbol1 c=blue i=join;

run; quit;



option ls=95;



/* Identification */



proc arima data=exp1;

identify var = x_t; run; quit;





/* Un deuxieme exemple d'un AR(2) stationnaire */



data exp1b;

c = -1; phi1 = 0.4; phi2 = - 0.4; x_t_1 = 0; x_t_2 = 0;

do i= -100 to 400;

	eps_t = rannor(32564);

	x_t   = c + phi1*x_t_1 + phi2*x_t_2 + eps_t;

	if i > 0 then output;

	x_t_2 = x_t_1;

	x_t_1 = x_t;

	end;

run;



proc gplot data = exp1b;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'AR(2: X_t= -1+ 0.4*X_(t-1) - 0.4*X_(t-2) + eps_t');

	symbol1 c=blue i=join;

run; quit;



proc arima data = exp1b;

identify var = x_t; run; quit;





/* Series simules: MA(p) */





data exp2;

c=0.8; theta1 = 0.3; theta2 = 0.4; eps_t_1 = 0; eps_t_2 = 0;

do i= -300 to 400;

	eps_t = rannor(32563);

	x_t   = c + eps_t - theta1*eps_t_1 - theta2*eps_t_2 ;

	if i > 0 then output;

		eps_t_2 = eps_t_1;

		eps_t_1 = eps_t;

	end;

run;



/* Graphique d'un MA(2) */

proc gplot data = exp2;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'MA(2): X_t =0.8+ eps_t - 0.3*eps_(t-1)-0.4*eps_(t-2)');

	symbol1 c=blue i=join;

run; quit;



/* Identification */

proc arima data = exp2;

identify var = x_t; run; quit;





/* Un deuxieme exemple d'un MA(2) stationnaire */

data exp2b;

c=0; theta1 = - 0.4; theta2 = 0.4; eps_t_1 = 0; eps_t_2 = 0;

do i= -300 to 400;

	eps_t = rannor(32563);

	x_t   = c + eps_t - theta1*eps_t_1 - theta2*eps_t_2 ;

	if i > 0 then output;

	eps_t_2 = eps_t_1;

	eps_t_1 = eps_t;

	end;

run;



proc gplot data = exp2b;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'MA(2): X_t = eps_t + 0.4*eps_(t-1)-0.4*eps_(t-2)');

	symbol1 c=blue i=join;

run; quit;



proc arima data = exp2b;

identify minic var = x_t; run; quit;





/* Series simules: ARMA(p,q) */



data exp3;

c=1.5; theta1 = - 0.5; theta2 = 0.2; phi1= 0.5; phi2= - 0.4;

eps_t_1 = 0; eps_t_2 = 0; x_t_1 =0; x_t_2=0;

do i= -400 to 500;

	eps_t = 1.5*rannor(32560);

	u_t   = eps_t - theta1*eps_t_1 - theta2*eps_t_2 ;

	x_t   = c + phi1*x_t_1 + phi2*x_t_2 + u_t;

	if i > 0 then output;

	x_t_2 = x_t_1;

	x_t_1 = x_t;

	eps_t_2 = eps_t_1;

	eps_t_1 = eps_t;

	end;

run;



proc gplot data = exp3;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'ARMA(2,2)');

	symbol1 c=blue i=join;

run; quit;



proc arima data= exp3;

identify minic var = x_t; run; 

estimate  p= ?  q= ?  ; run;



quit;







/*  Series simules: processus non stationnaire ou presque   */



/* AR(2) */

data exp4;

c=0.5; phi1 = 0.4; phi2 = 0.59; x_t_1 = 0; x_t_2 = 0;

do i= -100 to 400;

	eps_t = 2*rannor(32568);

	x_t   = c + phi1*x_t_1 + phi2*x_t_2 + eps_t;

	if i > 0 then output;

	x_t_2 = x_t_1;

	x_t_1 = x_t;

	end;

run;



proc gplot data = exp4;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'Processus non stationnaire');

	symbol1 c=blue i=join;

run; quit;



proc arima data = exp4;

identify var = x_t; run; quit;



/* un autre AR(2) */



data exp4b;

c=0.5; phi1 = 0.4; phi2 = 0.6; x_t_1 = 0; x_t_2 = 0;

do i= -100 to 400;

	eps_t = 2*rannor(32568);

	x_t   = c + phi1*x_t_1 + phi2*x_t_2 + eps_t;

	if i > 0 then output;

	x_t_2 = x_t_1;

	x_t_1 = x_t;

	end;

run;



proc gplot data = exp4b;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'Processus non stationnaire');

	symbol1 c=blue i=join;

run; quit;



proc arima data = exp4b;

identify var = x_t; run; quit;







/*  Series simules: processus MA non inversible   */



data exp5;

c=0.8; theta1 = 0.3;  eps_t_1 = 0; 

do i= -300 to 500;

	eps_t = rannor(32563);

	x_t   = c + eps_t - theta1*eps_t_1 ;

	if i > 0 then output;

	eps_t_1 = eps_t;

	end;

run;



proc gplot data = exp5;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'MA(2): X_t = eps_t + 0.4*eps_(t-1)-0.4*eps_(t-2)');

	symbol1 c=red i=join;

run; quit;



proc arima data = exp5;

identify var = x_t; run; quit;









/*  ESTIMATION */

/* AR(2) stationnaire */



data  exp1;

c=0.5; phi1 = 0.3; phi2 = 0.6; x_t_1 = 0; x_t_2 = 0;

do i= -100 to 400;

	eps_t = rannor(32565);

	x_t   = c + phi1*x_t_1 + phi2*x_t_2 + eps_t;

	if i > 0 then output;

	x_t_2 = x_t_1;

	x_t_1 = x_t;

	end;

run;





/* Graphique d'un AR(2) */

proc gplot data = exp1;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'AR(2): X_t=0.5 + 0.2*X_(t-1) + 0.4*X_(t-2) + eps_t');

	symbol1 c=blue i=join;

run; quit;



option ls=95;







/* Identification et estimation */



proc arima data = exp1;

identify var = x_t; run; 

estimate  p=2 ; run;

estimate  p=3 ; run;

estimate  p=2 q=1; run;

quit;







/*  ESTIMATION */

/* AR(1) non stationnaire */

data exp1b1;

c=0.5; phi1 = 0.99; x_t_1 = 0; 

do i= -100 to 400;

	eps_t = rannor(32565);

	x_t   = c + phi1*x_t_1  + eps_t;

	if i > 0 then output;

	x_t_1 = x_t;

	end;

run;



proc gplot data = exp1b1;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'AR(): X_t=0.5 + X_(t-1) + eps_t');

	symbol1 c=blue i=join;

run; quit;



proc arima data = exp1b1;

identify var = x_t; run; 

estimate  p=1 ; run;

quit;







/**********/



/* MA(2) inversible */

data  exp2;

c=0.8; theta1 = 0.3; theta2 = 0.4; eps_t_1 = 0; eps_t_2 = 0;

do i= -300 to 300;

	eps_t = rannor(32563);

	x_t   = c + eps_t - theta1*eps_t_1 - theta2*eps_t_2 ;

	if i > 0 then output;

	eps_t_2 = eps_t_1;

	eps_t_1 = eps_t;

	end;

run;





/* Identification et estimation */

proc arima data = exp2;

identify var = x_t; run; 

estimate  q=2 ; run;

quit;







/* MA(1) non inversible */

data exp5;

c=0.8; theta1 = 1;  eps_t_1 = 0; 

do i= -300 to 200;

	eps_t = rannor(32563);

	x_t   = c + eps_t - theta1*eps_t_1 ;

	if i > 0 then output;

	eps_t_1 = eps_t;

	end;

run;



proc gplot data = exp5;

	plot x_t*i / vref=0 vaxis=axis1;

	axis1 label=(angle=90 'MA(2): X_t = eps_t + 0.4*eps_(t-1)-0.4*eps_(t-2)');

	symbol1 c=blue i=join;

run; quit;



proc arima data = exp5;

identify var = x_t; run; 

estimate  q=1 ; run;

quit;

